package com.yumijinfu.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;

import com.yumijinfu.R;
import com.yumijinfu.base.BaseActivity;
import com.yumijinfu.entity.LoaginResult;
import com.yumijinfu.iview.ILoginView;
import com.yumijinfu.mian.MainActivity;
import com.yumijinfu.presenter.LoginPresenterCompl;
import com.yumijinfu.tool.CodeUtils;
import com.yumijinfu.view.ClearEditText;
import com.yumijinfu.view.TitleBar;

import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 登录
 */
public class LoginActivity extends BaseActivity implements ILoginView {
    private final static String TAG = "LoginActivity";
    @Bind(R.id.titlebar)
    TitleBar titlebar;
    @Bind(R.id.et_phone)
    ClearEditText etPhone;
    @Bind(R.id.et_password)
    ClearEditText etPassword;
    @Bind(R.id.et_code)
    ClearEditText etCode;
    @Bind(R.id.iv_code)
    //
    ImageView ivCode;   //生成验证码
    private String codeStr;
    private CodeUtils codeUtils;
    //
    private int flag;  //标识首页面从哪里进入登录页面
    //
    private LoginPresenterCompl loginPresenterCompl;
    //
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initTitle() {
        //
        flag = getIntent().getIntExtra("flag", 0);
        //
        codeUtils = CodeUtils.getInstance();
        titlebar.setTitleBar(R.drawable.back_selector, "登录", 0);
        titlebar.setTitleBarRightText("");
        titlebar.setTitleBarBackgroundColor(getResources().getColor(R.color.tou_ming));
        //记住账号
        String userAccount = (String) mpreferencesUtils.get(LoginActivity.this, "userAccount", "");
        if (null != userAccount) {
            etPhone.setText(userAccount);
        }
    }

    @Override
    protected void initView() {
        Bitmap bitmap = codeUtils.createBitmap();
        ivCode.setImageBitmap(bitmap);
        //
        loginPresenterCompl = new LoginPresenterCompl(this, this, this);
    }

    @Override
    protected void setData() {

    }


    @OnClick({R.id.tv_login, R.id.tv_lijizhuce, R.id.titlebar_iv_back, R.id.iv_code, R.id.tv_forget_password})
    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {
            case R.id.titlebar_iv_back:
                if (flag == 110) {   // 点击退出之后才调用
                    startActivity(new Intent(LoginActivity.this, MainActivity.class));
                    finish();
                }
//                if(flag==100){   //首页直接登录调用
//                    Intent intent=new Intent();
//                    setResult(100,intent);
//                    finish();
//                    return;
//                }

                //正常返回退出
                finish();
                break;
            case R.id.tv_forget_password:    // 忘记密码
                startActivity(new Intent(LoginActivity.this, ForgetPasswordActivity.class));
                break;

            case R.id.iv_code:    // 切换验证码
                ivCode.setImageBitmap(codeUtils.createBitmap());
                break;


            case R.id.tv_lijizhuce:  //立即注册
                startActivity(new Intent(LoginActivity.this, RegisteredSecondActivity.class));
                break;

            case R.id.tv_login:
                String phone = etPhone.getText().toString().trim();
                String password = etPassword.getText().toString().trim();
                String et_code = etCode.getText().toString().trim();
                //获取图形验证码
                codeStr = codeUtils.getCode();
                //使用mvp处理业务逻辑
                loginPresenterCompl.doLogin(phone, password, et_code, codeStr, etPhone, etPassword, etCode, flag);
                break;

            default:
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        if (event.getAction() == KeyEvent.ACTION_DOWN) {
//            if (flag == 110) {
//                startActivity(new Intent(LoginActivity.this, MainActivity.class));
//                finish();
//            }
//        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        if (flag == 110) {
            startActivity(new Intent(LoginActivity.this, MainActivity.class));
            finish();
        }
    }

    //mvp业务处理回调
    @Override
    public void Result(Object result) {
        if (null != result) {
            try {
                LoaginResult login = new LoaginResult(new JSONObject(result.toString()));
                mpreferencesUtils.put(LoginActivity.this, "token", login.getToken());
                mpreferencesUtils.put(LoginActivity.this, "jsessionid", login.getJSESSIONID());
                //
                //在已经登录成功 的情况下退出登录,就回到主页面(如退出登录)
                if (flag == 110) {
                    startActivity(new Intent(LoginActivity.this, MainActivity.class));
                    finish();
                } else if (flag == 100) {
                    Intent intent = new Intent();
                    setResult(110, intent);
                    finish();
                } else {
                    finish();
                }

            } catch (org.json.JSONException e) {
                e.printStackTrace();
            }

        }


    }
}
